package com.classic.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.Calendar;


/**
 * username:Created by hsj on 2016/12/7 0007.
 * class：
 * 类描述：
 */

public abstract class BaseFragment extends Fragment implements View.OnClickListener {
    protected Context context;
//    RefreshLayout smartLayout;

    protected abstract int getLayoutResourceId();

    protected abstract void initView();

    protected abstract void initData();

    public View rootView;

    public BaseFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public static final int MIN_CLICK_DELAY_TIME = 1000;
    private long lastClickTime = 0;

    @Override
    public void onClick(View v) {
        long currentTime = Calendar.getInstance().getTimeInMillis();
        if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
            lastClickTime = currentTime;
            onNoDoubleClick(v);
        }
    }

    public void onNoDoubleClick(View v) {

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null != rootView) return rootView;
        rootView = inflater.inflate(getLayoutResourceId(), container, false);
        context = getContext();
        initSmartLayout();
        initView();
        initData();
        return rootView;
    }

    /**
     * 初始化刷新控件
     */
    protected void initSmartLayout() {
//        smartLayout = rootView.findViewById(R.id.smartLayout);
//        if (null != smartLayout) {
//            smartLayout.setOnRefreshListener(this);
//            smartLayout.setEnableLoadmore(false);
//        }
    }

    /**
     * 结束下拉刷新
     */
    protected void stopRefresh() {
//        if (null != smartLayout)
//            smartLayout.finishRefresh();
    }

    /**
     * 结束下拉刷新
     */
    protected void autoRefresh() {
//        if (null != smartLayout)
//            smartLayout.autoRefresh();
    }

    protected void showTT(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen",
                "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

}
